/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Endlesslove
 */
public class HoaDon implements IDaTa{

    @Override
    public ResultSet Search(DTO.Idata Obj) 
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  ((DTO.HoaDon)Obj).getID()) ;//masach
            return DAO.CallProc("LayThongTinHoaDon", params);
            
        } 
        catch (Exception ex) 
        {
            return null;
        }
    }

    @Override
    public boolean Update(DTO.Idata Obj) 
    {
            return false;
    }

    @Override
    public boolean Delete(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" + ((DTO.HoaDon)Obj).MaKhachHang);
        return DAO.CallProc("XoaHoaDon", params, true);
    }
    
     public ResultSet GetAll()
    {
        try
        {
            return DAO.CallProc("ThongTinToanBoHoaDon");   
        } 
        catch (Exception ex) 
        {
            return null;
        }
    }
    @Override
    public void LoadData(Object table) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Insert(DTO.Idata Obj) 
    {
        
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.HoaDon)Obj).getID()) ;//ma 
        params.add("Date:" +  ((DTO.HoaDon)Obj).NgayLap);//Ngay Hoa Don
        params.add("char:" +  ((DTO.HoaDon)Obj).MaKhachHang );//Ma Khach Hang
        params.add("int:" +  ((DTO.HoaDon)Obj).TongTien);//Tong Tien
        return DAO.CallProc("ThemHoaDon", params, true);
    }
    
    @Override
    public String GenerateID()
    {
        try 
        {
            ResultSet res = DAO.CallProc("LayMaHoaDonCuoi");
            if (res.next())
            {    
              int index  = Integer.parseInt(((String)res.getObject(1)).split("HD")[1].trim());
              String ms = "HD";
              ms += (index+1)>=10?(index+1)>=100? (index +1) : "0" + (index +1) : "00" + (index + 1);
              return ms;
            }
            return "HD001";
        } 
        catch (SQLException ex) 
        {
            return "HD001";
        }
        catch(Exception ie)
        {
            return "HD001";
        }
    }
    
    public boolean DeleteHD(String MHD)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  MHD) ;//masach
            return DAO.CallProc("XoaHoaDon", params, true);
            
        } 
        catch (Exception ex) 
        {
            return false;
        }
    }
}
